use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
    pub app: AppCfg,
    pub db: DbConfig,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct AppCfg {
    pub addr: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct DbConfig {
    pub dsn: String,
    pub max_conns: u32,
}

impl Config {
    pub fn from_env() -> Result<Self, ::config::ConfigError> {
        ::config::Config::builder()
            .add_source(config::Environment::default())
            .build()?
            .try_deserialize()
    }
}
